package com.itheima.reggie.filter;


import com.alibaba.fastjson.JSON;
import com.itheima.reggie.common.R;
import com.itheima.reggie.utils.BaseContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.AntPathMatcher;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 拦截器
 * 1.判断用户是否登录
 * 2.判断用户访问的资源是需要放过
 *
 *
 */
@Slf4j
@WebFilter(filterName = "LoginCheckFilter", urlPatterns = "/*")
public class LoginCheckFilter implements javax.servlet.Filter {
    // 路径匹配器  /**
    public static final AntPathMatcher PATH_MATCHER=new AntPathMatcher();

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        //转化为httpRequest 与 httpresponse
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;

        //获取请求路径
        String requestURI = request.getRequestURI();
        log.info("拦截到的请求为{}"+requestURI);

        /**
         *  需要放过的资源
         * 登录 登出
         * 静态资源
         */
        String[] uris={
            "/employee/login",
            "/employee/logout",
             "/backend/**",
             "/front/**",
             "/common/**",
             "/user/sendMsg",
             "/user/login"
        };

        //根据请求路径来判断数组里面是否有放过的资源
        boolean flag = check(uris,requestURI);
        if(flag){
            log.info("放过的资源为:{}"+requestURI);
            filterChain.doFilter(request,response);
            return ;
        }

        /**
         *  判断用户是否登录
         */
        if(request.getSession().getAttribute("employee") != null){
            log.info("用户已登录");
            Long empId = (Long) request.getSession().getAttribute("employee");
//            threadLocal.set(empId);
            BaseContext.set(empId);
            log.info("用户登录的id为:{}",empId);
            filterChain.doFilter(request,response);
            return ;
        }

        //4-2、判断登录状态，如果已登录，则直接放行
        if(request.getSession().getAttribute("user") != null){
            log.info("用户已登录，用户id为：{}",request.getSession().getAttribute("user"));

            Long userId = (Long) request.getSession().getAttribute("user");
            BaseContext.set(userId);

            filterChain.doFilter(request,response);
            return;
        }
        response.getWriter().write(JSON.toJSONString(R.error("NOTLOGIN")));
    }




    //判断访问的资源是否需要放过
    private boolean check(String[] uris, String requestURI) throws IOException, ServletException {
        for (String uri : uris) {
            boolean match = PATH_MATCHER.match(uri, requestURI);
            if(match){
               return true;
            }
        }
        return false;
    }
}
